const qiniu = require('qiniu')
import conf from '../../config'
const accessKey = conf.qiniu.accessKey;
const secretKey = conf.qiniu.secretKey;
const scope = conf.qiniu.scope;
const expires = conf.qiniu.expires;
const options = { scope, expires }
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const putPolicy = new qiniu.rs.PutPolicy(options)

module.exports = {
  async getQiniuToken (ctx, next) {
      try {
        const uploadToken = putPolicy.uploadToken(mac)
        ctx.resResult({data: uploadToken}, '七牛上传凭证创建成功',)
        // ctx.body = ({code: 'ok', message: '七牛上传凭证创建成功', data: uploadToken})
      } catch (e) {
        ctx.resResultError('七牛上传凭证创建失败');
        // ctx.body = ({code: 'no', message: '七牛上传凭证创建失败'})
      }
    }
}
